//得到filesize字符显示
export const getFileSizeStr = (size: number) => {
  const kb_size = size / 1024
  const mb_size = kb_size / 1024
  const gb_size = mb_size / 1024
  if (gb_size > 1) {
    return gb_size.toFixed(2) + ' GB'
  } else if (mb_size > 1) {
    return mb_size.toFixed(2) + ' MB'
  } else if (kb_size > 1) {
    return kb_size.toFixed(2) + ' KB'
  } else if (size >= 0) {
    return  size + 'B'
  }
  return '';
}
export const throttle = function (func:any, wait = 200, immediate = false) {
  let timeout:any
  return function (this:any,...args:any[]) {
    if (!timeout) {
      const context:any = this
      if (immediate) {
        func.apply(context, args)
        timeout = window.setTimeout(function () {
          timeout = null
        }, wait)
      } else {
        timeout = window.setTimeout(function () {
          timeout = null
          func.apply(context, args)
        }, wait)
      }
    }
  }
}

//得到filesize字符显示
export const getRealSizeByStr=(size:string)=>{
  const sizeStr = size.trim().toUpperCase();
  const value = parseFloat(sizeStr);//得到前面的数字
  if (sizeStr.endsWith('GB')) {
    return parseFloat((value * 1024 * 1024 * 1024).toFixed(2));
  } else if (sizeStr.endsWith('MB')) {
    return parseFloat((value * 1024 * 1024 ).toFixed(2));
  }else if (sizeStr.endsWith('KB')) {
    return parseFloat((value * 1024  ).toFixed(2));
  }else if (sizeStr.endsWith('B')) {
    return parseFloat((value ).toFixed(2));
  }
}

//获取剩余时间
export const getNeedTime = (time: number) => {
  time = time >> 0;
  var day=Math.floor(time/(3600*24))
  var hours = Math.floor((time%(3600*24)) / 3600);
  var minutes = Math.floor((time % 3600) / 60);
  var seconds = time % 60;
  const str = [hours, minutes, seconds].map(value => String(value).padStart(2, '0')).join(' : ');
  if (day) {
    // return day + ' 天 ' + str;
    return '大于1天'
  } else {  
    return str;
  }
}

export const parseJSONstr = (str:string):any => {
  let obj = {};
  try { 
    obj = JSON.parse(str);
  } catch (ex) {
    obj = {};
  }
  return obj;
}